package com.zhangyu.shadow.repair.delegate.search;


import com.zhangyu.shadow.repair.base.IStringDataConvert;
import com.zhangyu.shadow.repair.config.ErrorMsg;
import com.zhangyu.shadow.repair.delegate.bean.BusBean;
import com.zhangyu.shadow.repair.delegate.bean.BusTypeBean;

import org.greenrobot.greendao.annotation.NotNull;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.observers.DisposableObserver;
import io.reactivex.schedulers.Schedulers;
import st.lib.base.net.callback.IError;
import st.lib.base.ui.recycler.MultipleItemEntity;

/**
 * Description:
 *
 * @author Hequn.Lee
 * @date 2018/11/7
 */
public class SearchBusPresenter extends SearchBusContract.Presenter {

    private final SearchBusContract.View mView;
    private final SearchBusContract.Model mModel;
    /**
     * 是否正在请求
     */
    private boolean isRefreshing = false;

    public SearchBusPresenter(@NotNull SearchBusContract.View view) {
        mView = view;
        mModel = new SearchBusModel();
    }

    @Override
    void searchBus(String code) {
        if (isRefreshing) {
            return;
        }
        if (mView != null) {
            mView.showDialogProgress();
        }
        addDisposable(mModel.searchCode(code, new IStringDataConvert<ArrayList<MultipleItemEntity>>() {
                    @Override
                    public void convert(ArrayList<MultipleItemEntity> list) {
                        if (mView != null) {
                            mView.hideLoading();
                            mView.searchBusSuccess(list);
                        }
                        isRefreshing = false;
                    }
                }, new IError() {
                    @Override
                    public void onError(int code, String msg) {
                        if (mView != null) {
                            mView.searchBusFailed(code, msg);
                        }
                        isRefreshing = false;
                    }
                })
        );
    }

    @Override
    void searchCode(String code) {
        if (isRefreshing) {
            return;
        }
        if (mView != null) {
            mView.showLoading("");
        }
        addDisposable(mModel.searchCode(code, new IStringDataConvert<ArrayList<MultipleItemEntity>>() {
                    @Override
                    public void convert(ArrayList<MultipleItemEntity> list) {
                        if (mView != null) {
                            mView.hideLoading();
                            mView.searchCodeSuccess(list);
                        }
                        isRefreshing = false;
                    }
                }, new IError() {
                    @Override
                    public void onError(int code, String msg) {
                        if (mView != null) {
                            mView.searchCodeFailed(code, msg);
                        }
                        isRefreshing = false;
                    }
                })
        );
    }

    /**
     * final String key, String groupId,
     *
     * @param dateStart 开始时间
     * @param dateEnd   结束时间
     * @param depId     分厂ID
     */
    @Override
    void getBusList(String dateStart, String dateEnd, String depId) {
        if (isRefreshing) {
            return;
        }
        if (mView != null) {
            mView.showLoading("");
        }
        addDisposable(mModel.getBusList(depId,
//                 groupId,
                dateStart, dateEnd, new IStringDataConvert<ArrayList<BusBean>>() {
                    @Override
                    public void convert(ArrayList<BusBean> list) {
                        if (list != null) {
                            convertData(list);
                        } else {
                            if (mView != null) {
                                mView.getBusListFailed(-1, ErrorMsg.ERROR_DATA);
                            }
                        }
                        isRefreshing = false;
                    }
                }, new IError() {
                    @Override
                    public void onError(int code, String msg) {
                        if (mView != null) {
                            mView.getBusListFailed(code, msg);
                        }
                        isRefreshing = false;
                    }
                })
        );
    }

    private void convertData(final ArrayList<BusBean> list) {
        addDisposable(Observable.create(new ObservableOnSubscribe<List<BusTypeBean>>() {
            @Override
            public void subscribe(ObservableEmitter<List<BusTypeBean>> emitter) throws Exception {
                List<BusTypeBean> typeBeanList = new ArrayList<>();
                ArrayList<BusBean> allList = new ArrayList<>();
                ArrayList<BusBean> underPlanList = new ArrayList<>();
                ArrayList<BusBean> finishPlanList = new ArrayList<>();
                ArrayList<BusBean> completeList = new ArrayList<>();
                for (BusBean bean : list) {
//                    if (bean.getVehicleCode().contains("")) {
                    allList.add(bean);
                    switch (bean.getWorkStatus()) {
                        case 4:
                            //UnderWork
                            underPlanList.add(bean);
                            break;
                        case 5:
                            //CompletedWork
                        case 6:
                            //FinishWork
                            completeList.add(bean);
                            break;
                        default:
                            finishPlanList.add(bean);
                            break;
                    }
//                    }
                }
                typeBeanList.add(new BusTypeBean(allList.size(), "全部", allList));
                typeBeanList.add(new BusTypeBean(finishPlanList.size(), "待处理", finishPlanList));
                typeBeanList.add(new BusTypeBean(underPlanList.size(), "处理中", underPlanList));
                typeBeanList.add(new BusTypeBean(completeList.size(), "已完成", completeList));
                emitter.onNext(typeBeanList);
            }
        }).observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribeWith(new DisposableObserver<List<BusTypeBean>>() {
                    @Override
                    public void onNext(List<BusTypeBean> typeBeanList) {
                        if (mView != null) {
                            mView.hideLoading();
                            mView.getBusListSuccess(typeBeanList);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                }));
    }
}
